<template>
  <el-dialog v-model="isShowDialog"
             :before-close="handleClose"
             :title="title"
             draggable
             width="500px"
             append-to-body
  >
    <el-form :model="saveParams" ref="formRef" label-width="100px" :rules="rules">
      <el-form-item label="快递编号" prop="expressCode">
        <el-input v-model="saveParams.expressCode" @input="saveParams.expressCode=limitCharAndNumber(saveParams.expressCode)" :disabled="isView" placeholder="快递编号" clearable/>
      </el-form-item>
      <el-form-item label="快递公司" prop="expressName">
        <el-input v-model="saveParams.expressName" :disabled="isView" placeholder="快递公司" clearable/>
      </el-form-item>
      <el-form-item label="所属仓库" prop="warehouseId">
        <el-select
            :disabled="saveParams.expressId"
            clearable
            v-model="saveParams.warehouseId"
        >
          <template v-for="(item,index) in warehouseList" :key="index">
            <el-option :label="item.warehouseName" :value="item.warehouseId"></el-option>
          </template>
        </el-select>
      </el-form-item>
      <el-form-item label="网址">
        <el-input v-model="saveParams.url" :disabled="isView" placeholder="网址" clearable/>
      </el-form-item>
      <el-form-item label="联系人">
        <el-input v-model="saveParams.linkman" :disabled="isView" placeholder="联系人" clearable/>
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="saveParams.phone" @input="saveParams.phone=limitIntNumber(saveParams.phone)" :maxlength="11" :disabled="isView" placeholder="电话" clearable/>
      </el-form-item>
      <el-form-item label="地址">
        <el-input v-model="saveParams.address" :disabled="isView" placeholder="地址" clearable/>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="saveParams.remark" :disabled="isView" placeholder="备注" clearable/>
      </el-form-item>

    </el-form>

    <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="clickSave" v-if="!isView"> 保存 </el-button>
          <el-button @click="handleClose">取消</el-button>
        </span>
    </template>

  </el-dialog>
</template>

<script setup>
import {getCurrentInstance, onMounted} from "vue";
import {expressSave} from "@/api/scApi";
import {limitCharAndNumber} from "@/utils/shuangcheng";
import {getWarehouseSelect} from "@/api/system/user";

const {proxy} = getCurrentInstance();
const props = defineProps({
  isShowDialog: {
    type: Boolean,
    default: false
  },
  title: {type: String},
  rowData: {type: Object},
  isView: {type: Boolean}
})
const emits = defineEmits(['cancel'])

function handleClose() {
  emits('cancel')
}

let saveParams = ref(JSON.parse(JSON.stringify(props.rowData)))
onMounted(() => {
  getWarehouse()
  if (!saveParams.value.status) {
    saveParams.value.status = 0
  }

})
// 表单校验
const rules = ref({
  expressCode: [{required: true, message: "快递编号不能为空", trigger: "blur"}],
  expressName: [{required: true, message: "快递公司不能为空", trigger: "blur"}],
  warehouseId: [{required: true, message: "仓库不能为空", trigger: "change"}],
});

function clickSave() {
  proxy.$refs.formRef.validate((valid, fileds) => {
    if (valid) {
      expressSave(saveParams.value).then(res => {
        if (res.code === 200) {
          proxy.$modal.msgSuccess('保存成功')
          emits('cancel', true)
        }
      })
    }
  })
}
const sysUserInfo=JSON.parse(localStorage.getItem("userInfo"))
//获取仓库下拉数据
let warehouseList = ref([])
function getWarehouse(){
  getWarehouseSelect({'companyId':sysUserInfo.companyId}).then(res => {
    warehouseList.value = res.data
  })
}

</script>

<style scoped lang="scss">

</style>